Count the objects in an image
Use this recipe when the inspection has to say how many objects there are, or whether one is missing: the biscuits in a pack, the caps on a tray, the screws on a part. The model has to find each object, so you need an object detection dataset, with a box around every object. Choose oriented boxes when the objects lie at an angle, because a straight box around a tilted object takes in a lot of background (Label rotated objects with oriented boxes). A segmentation model counts the same way, but masks take longer to label, and boxes are enough to count.
| Step | Choice |
|---|---|
| Dataset | «Object detection», with normal or oriented boxes |
| Labeling | Auto Labeling with a prompt, then a check in «Validate» |
| Results | «Count matching metrics» |
| Threshold | The confidence that keeps the count right, with a margin |
| Flow | Count the predictions above the threshold |
Collect the images
Create a dataset of type «Object detection». Collect products with the right count and, above all, the cases the line must catch: an object missing, an extra one, objects touching or partly covering each other. Use the line's camera and light.
Each tag needs at least 10 boxes to be trained, and only reviewed images go into training: an image with boxes counts as reviewed, and images still «Pending» are left out. If the images are large and the objects small, «Image Slicing» trains on tiles, so each object takes up more pixels (Adjust image size, slicing and advanced settings).
Label them
Box every object in every image. An object left without a box teaches the model that nothing is there.
- Boxes from another tool. Upload a ZIP with a
.txtfile next to each image, one line per box:tag x1 y1 x2 y2, with corners from 0 to 1 (Upload a ZIP with tags and annotations). - Objects a general model knows. Auto Labeling finds objects from a short, precise description: in its guide, «ladyfinger» finds the biscuits clearly where «biscuit» does not. Click «Apply» to keep the boxes, then turn on «Auto» to go through the images (Label automatically with a text prompt).
- One click per object. In detection datasets, Smart Labeling draws a box. You can also draw boxes by hand (Label objects with bounding boxes).
- Once a model works. Let it pre-label each new batch, and correct its mistakes (Pre-label images with a trained model).
Then check: in the «Validate» tab, the «Images» view covers every box with the «Cover colour», so any object nobody labeled still shows (Review and validate annotations).
Train and read the results
In «Models», start a «Production training» (Train your first model). In detection datasets, the platform first suggests validating the boxes; click «Continue» to go on.
When the training ends, choose «Count matching metrics» in «Evaluation type». An image counts as right when the model finds as many objects of each tag as were labeled, and «Undercount» and «Overcount» say which way it went wrong. Open a red image and click «Compare with the manual annotation»: a missing or extra object stands out at once (Read the results of a detection or segmentation model).
Next, in «Evaluation images», move «Confidence threshold» and watch which images turn red. Pick a value inside the range where most images are right, with a margin on both sides (Choose the confidence threshold of a model). The threshold is not saved with the model, so you set it in the flow.
Count in a flow
The inferencer node puts a list in msg.inference, with one entry per object, each with its tag and a confidence from 0 to 1 (Run a trained model on an image). A function node counts them:
if (!Array.isArray(msg.inference)) return null;
const found = msg.inference.filter((d) => d.confidence >= 0.5).length;
msg.payload = found === 4 ? "OK" : "NOK";
msg.topic = found + " found";
return msg;Replace 4 with your count and 0.5 with your threshold; to count only one tag, filter on d.tag too. The first line skips images the model could not judge, such as those arriving while it loads. Stream the whole dataset through the rule before going live, then add a switch node to send NOK products to the reject (Decide pass or fail from the predictions). Build your first inspection flow builds this exact flow.
Show it to the operators
rp-add-bbs draws each box on the image with its tag and confidence (Draw results on the image). On a panel, the image canvas accepts the boxes of a detection model in imageShow.<type>.bbox, and its result card shows OK or NOK (Show inspection results on a panel). A «Minimum confidence» drop-down list stores the operator's choice in the flow context, and the rule reads it with flow.get instead of a fixed 0.5 (Add buttons and selectors to a panel).
Improve it
- Images from the line. Save the NOK products and upload them with
dataset-upload. They arrive without boxes: let your model pre-label them, then correct them (Collect images from a flow into a dataset). - Label or model? A box missing from the label is fixed in the label. An object the model missed needs more images like it (Find where a model fails and fix it).
- Train again. Use «Copy from another training», or «Train from model» where it is offered. Check the threshold again after each training, because a new model gives different confidences (Retrain a model after adding images).